package com.micro.frog.acct.rest;

import com.micro.frog.acct.dto.RoleDTO;
import com.micro.frog.acct.dto.UserDetail;
import com.micro.frog.acct.entity.Role;
import com.micro.frog.acct.service.RoleService;
import com.micro.frog.acct.utils.SubjectUtil;
import com.micro.frog.core.common.PageResult;
import com.micro.frog.core.common.PageVO;
import com.micro.frog.core.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 角色操作
 * @author :  ljh
 * @date : 2018/4/12
 **/
@Slf4j
@RestController
@RequestMapping(value = "/rbac/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    /**
     * 带条件查询带分页
     */
    @GetMapping(value = "/list")
    public PageResult listPage(RoleDTO pageVO) {
        PageResult result = new PageResult();
        try {
            result = roleService.listPage(pageVO);
        } catch (Exception e) {
            result.setCode("20000");
            result.setMsg("系统异常,{}" + e.getMessage());
            log.error("系统异常 e={}", e);
        }
        return result;
    }

    /**
     * 查询全部不带分页
     *
     * @return
     */
    @GetMapping(value = "/all")
    public Result nopage(Role role) {
        Result result = new Result();
        try {
            result.setFlag(true);
            result.setData(roleService.all(role));
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("系统异常");
            log.error("系统异常 e={}", e);
        }
        return result;
    }

    @PostMapping("/add")
    public Result add(HttpServletRequest request, Role role) {
        Result result = new Result();
        try {
            UserDetail user = SubjectUtil.getUser(request);
            role.setCreatedBy(user.getUserName());
            roleService.add(role);
            result.setFlag(true);
            result.setMsg("新增成功");
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("新增失败");
            log.error("新增失败 e={}", e);
        }
        return result;
    }

    @PostMapping("/delete")
    public Result deleteDict(Integer id) {
        Result result = new Result();
        try {
            roleService.delete(id);
            result.setFlag(true);
            result.setMsg("删除成功");
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("系统异常");
            log.error("系统异常 e={}", e);
        }
        return result;
    }

    @PostMapping("/update")
    public Result update(HttpServletRequest request, Role role) {
        Result result = new Result();
        try {
            UserDetail user = SubjectUtil.getUser(request);
            role.setUpdatedBy(user.getUserName());
            roleService.update(role);
            result.setFlag(true);
            result.setMsg("更新成功");
        } catch (Exception e) {
            result.setFlag(false);
            result.setMsg("系统异常");
            log.error("系统异常 e={}", e);
        }
        return result;
    }
}
